import requests
from loguru import logger


def login():
    url = 'https://api.admin.merchant.java.crmeb.net/api/admin/platform/login'
    headers = {'content-type': 'application/json'}
    json_data = {
        'account': 'demo',
        'pwd': 'crmeb.com',
        'captchaVO': {},
    }
    response = requests.post(url, headers=headers, json=json_data)
    return response.json()


class BaseRequest:
    def __init__(self):
        self.token = login()['data']['token']
        self.session = requests.Session()
        self.session.headers.update({'authori-zation': self.token, 'content-type': 'application/json'})

    def post(self, url, json_data=None):
        logger.debug(f'接口请求： {url}')
        response = self.session.post(url, json=json_data)
        logger.debug(f'接口响应： {response.text}')
        return response.json()

    def get(self, url):
        logger.debug(f'接口请求： {url}')
        response = self.session.get(url, params=None)
        logger.debug(f'接口响应： {response.text}')
        return response.json()
